﻿using Microsoft.JScript.Vsa;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace RoleSelection.Helper
{
    public static class RespHelper
    {
        static VsaEngine Engine = VsaEngine.CreateEngine();

        public static RespInfo GetRespInfo(string respStr)
        {
            var eval = respStr.Substring(respStr.IndexOf('=') + 1).Trim();
            eval = eval.Substring(0, eval.Length - 1);
            eval = eval.Replace("'", "\"");
            eval = Regex.Replace(eval, "([{]|[,])([\\s]*)([\\w]+?)([\\s]*)([:])", "$1\"$3\"$5");
            var respInfo = JsonConvert.DeserializeObject<RespInfo>(eval);
            respInfo.DataDict = BulidDict(respInfo.Data); ;
            
            
            return respInfo;
        }

        private static Dictionary<string,string> BulidDict(string dataStr)
        {
            var dict = new Dictionary<string, string>();
            var arr = dataStr.Split('&');
            foreach (var item in arr)
            {
                if (item.Contains('='))
                {
                    var pair = item.Split('=');
                    dict.Add(pair[0], pair[1]);
                }
            }
            return dict;
        }
    }
}
